var pickerFile = require('../../../../../../interest/lib/picker_datetime/js/picker_datetime.js');
const app = getApp()
const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
Page({
  data: {
    circleid: "766",
    checkGoods: false,//判断商品中是否有宝贝类型的商品
    wayType:1,//1配送到家，2到店自提，3预约服务
    addrData:[],//用户地址信息
    addrDefault:{},//用户默认地址
    showDetail: null,//需要展开的备注面板
    isHidePickerDatetime: true,// 是否隐藏时间的选择
    minSendTime: '',//最开配送时间
    sendTime: '',//选择的配送时间
    claimTime: '',//取货时间
    multiArray: [["今日"],[]],
    circleRestTime: '22:00',
    circleBeginTime: '08:00',
    goodsData: [
      { "title": "标题三十字标题三十字标题三十字标题三十字标题三十字标题三十字", "contentType": 1, 'goodsSonData': [{ "price": 19, 'cycle': "单次", "sum": 1, "labelName": '' }],'ps':"这是添加后备注，限五十个字符！费用周期不限是指一次购买后可以多次是有（直至圈子活动结束）"},
      { "title": "速记阅读练习手册有大中小三种规格供选择优惠中（限三十个字符）", "contentType": 4, "deliveryType": 1, 'goodsSonData': [{ "price": 39, 'cycle': "单次", "sum": 1, "labelName": '中码' }, { "price": 100, 'cycle': "单次", "sum": 2, "labelName": '大码' }]}, 
    ]
  },
  onLoad: function (options) {
    var _this = this;
    var checkReslut = _this.checkoutGoods();
    console.log(checkReslut)
    _this.setData({
      checkGoods: checkReslut
    })
    _this.datetimePicker = new pickerFile.pickerDatetime({
      page: _this,
      animation: 'slide',
      duration: 500
    });
    //设置最快的配送时间
    var minSendTime = util.haveSomeMinutesTime(1440);
    _this.setData({
      minSendTime: minSendTime,
      sendTime: minSendTime
    })
    _this.getCircleDetails();
  },
  onShow: function(){
    var _this = this;
    _this.getAddr();
  },
  /**
   * 判断商品中是否有宝贝类型
   */
  checkoutGoods: function(){
    var _this = this;
    var arr = _this.data.goodsData;
    var conut = 0
    for(var i=0;i<arr.length;i++){
      if (arr[i].contentType == 4 || arr[i].contentType == 5){
        conut++;
      }
    }
    if (conut>0){
      return true
    }else{
      return false
    }
  },
  /**
   * 选择配送类型
   */
  switchPanel:function(e){
    var _this = this;
    var id = e.currentTarget.dataset.id;
    _this.setData({
      wayType: id
    })
  },
  /**
   * 展开备注面板
   */
  openDetail:function(e){
    var _this = this;
    var show = _this.data.showDetail;
    var index = e.currentTarget.dataset.index;
    if(show==index){
      _this.setData({
        showDetail: null
      })
    }
    else{
      _this.setData({
        showDetail: index
      })
    } 
  },
  /**
   * 获取收货地址
   */
  getAddr: function(){
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.GetBasAddressByUser, data).then(function (res) {
          if (res.code == 200) {
            if (res.result.list.length > 0) {
              for (var i = 0; i < res.result.list.length;i++){
                if (res.result.list[i].isDefault==1){
                  var addrDefault = res.result.list[i]
                }
              }
              _this.setData({
                addrData: res.result.list,
                addrDefault: addrDefault,
              })
              console.log(addrDefault)
            }
          }
        });
      });
    }
  },
  /**
   * 使用微信内置地图查看位置
   */
  wxGetLocation: function (e) {
    var name = e.currentTarget.dataset.name;
    var address = e.currentTarget.dataset.address;
    var latitude = e.currentTarget.dataset.latitude;
    var longitude = e.currentTarget.dataset.longitude;
    wx.openLocation({
      latitude: latitude,//要去的纬度-地址
      longitude: longitude,//要去的经度-地址
      name: name,
      address: address
    })
  },

  /**
   * 跳转地址设置页
   */
  gotoSetaddr: function(){
    wx.navigateTo({
      url: '/ucenter/pages/ucenter/personinfo/homesetup/addrsetup/addrsetup',
    })
  },
  /**
   * 选择送达时间
   */
  setSendTime: function () {
    var _this = this;
    console.log(_this.data.minSendTime)
    _this.datetimePicker.setPicker("sendTime", _this.data.minSendTime);
    _this.setData({
      isHidePickerDatetime: false
    })
  },
  /**
   * 选择取货时间
   */
  multiArray: [["今日"], []],
  circleRestTime: '22:00',
  circleBeginTime: '08:00',
  bindMultiPickerColumnChange: function (e){
    var _this = this;
    var time = e.detail.value
    _this.setData({
      claimTime: time
    })
  },
  //获取下级
  bindMultiPickerColumnChange: function (e) {
    var newDate = util.getDateStr(new Date(), 0)
    console.log(newDate)
    this.setData({

    })
  },
  /**
   * 获取圈子信息
   */
  getCircleDetails: function () {
    var _this = this;
    var circleid = _this.data.circleid;
    var data = {};
    data.circleid = circleid;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      // 已登录
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        _this.getCircleDetailsQuery(api.Home_GetCircleDetailsApiUrl, data);
      })
    } else {
      _this.getCircleDetailsQuery(api.Home_GetCircleDetailsUrl, data);
    }
  },
  /**
   * 获取圈子信息：请求
   */
  getCircleDetailsQuery: function (url, data) {
    var _this = this;
    util.post(url, data).then(function (res) {
      if (res.code == 200) {
        var json = res.result.json;
        if (json != null) {
          console.log(json)
          _this.setData({
            albumInfo: json.albumInfo,
            mainUser: json.mainUser,
            ordContractList: json.ordContractList,
            ordInterest: json.ordInterest,
            energySize: json.energySize,
            ordInterestJoinCount: json.ordInterestJoinCount,
            collectionUserCount: json.collectionUserCount,
            collectionTotalCount: json.collectionTotalCount,
            diaryCount: json.diaryCount,
            thisInterestJoinUser: json.thisInterestJoinUser,
            mainUserSetup: json.mainUserSetup,
            title: json.ordInterest.title,
            mainBasHomeinfo: json.mainBasHomeinfo
          })
        }
      }
    });
  },
  /**
   * 获取送货时间段
   */
  getClaimTime: function(){
    var _this = this;
    var timeList = _this.data.multiArray;
    var ordInterest = _this.data.ordInterest;
    var now = new Date();
    var isClock = false;
    var projectdateIdentification = ordInterest.projectdateIdentification;
    if (projectdateIdentification == 0) {
      var day1 = util.getDateStr(new Date(), 0)
      console.log(day1)
    } else {
      var nowTime = now.getTime();
      var projectdateStart = new Date(ordInterest.projectdateStart.replace(/-/g, '/')).getTime();
      var projectdateEnd = new Date(ordInterest.projectdateEnd.replace(/-/g, '/')).getTime();
    }
  }
})